﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyBlog.Model;
using MyBlog.DataAccess;
using System.Linq.Expressions;
using MyBlog.Common;
using AutoMapper;
using MyBlog.BusinessInterface;
using MyBlog.DataEntity;
using MyBlog.DataAccessInterface;
namespace MyBlog.Business
{
    public class BusinessTag : IBusinessTag
    {
        IDALTag _dalTag;
        IUnitOfWork _unitWork;
        public BusinessTag(IDALTag dalCategory, IUnitOfWork unitWork)
        {
            _dalTag = dalCategory;
            _unitWork = unitWork;
        }
        public void Add(TagModel model)
        {
            var dataEntity = Mapper.Map<Tbl_Tag>(model);
            _dalTag.Add(dataEntity);
            _unitWork.Commit();
            model.ID = dataEntity.ID;
        }

        public void Update(TagModel model)
        {
            var tag = _dalTag.Get(model.ID);
            tag.TagName = model.TagName;
            _unitWork.Commit();
        }

        public List<TagModel> GetAll(bool isTracking = true)
        {
            var entities = _dalTag.GetAll(isTracking);
            return Mapper.Map<List<TagModel>>(entities);
        }

        public void Delete(params object[] primaryKey)
        {
            var entity = _dalTag.Get(primaryKey);
            entity.Tbl_Article.Clear();
            _dalTag.Delete(primaryKey);
            _unitWork.Commit();
        }

        public TagModel Get(params object[] primaryKey)
        {
            var dataEntity = _dalTag.Get(primaryKey);
            return Mapper.Map<TagModel>(dataEntity);
        }

        public void Delete(TagModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Tag>();
            if (model.ID > 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            _dalTag.Delete(where);
            _unitWork.Commit();
        }

        public TagModel Get(TagModel model)
        {
            var where = LinqExpressionBuilder.True<Tbl_Tag>();
            if (model.ID != 0)
            {
                where = where.And(x => x.ID == model.ID);
            }
            if (!string.IsNullOrWhiteSpace(model.TagName))
            {
                where = where.And(x => x.TagName.ToLower() == model.TagName.ToLower());
            }
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            var dataEntity = _dalTag.Get(where);
            return Mapper.Map<TagModel>(dataEntity);
        }

        public List<TagModel> GetAll(TagModel model, bool isTracking = true)
        {
            var where = LinqExpressionBuilder.True<Tbl_Tag>();
            if (!string.IsNullOrWhiteSpace(model.AccountId))
            {
                where = where.And(x => x.AccountId.ToLower() == model.AccountId.ToLower());
            }
            if (!string.IsNullOrWhiteSpace(model.TagName))
            {
                where = where.And(x => x.TagName.ToLower() == model.TagName.ToLower());
            }
            var entities = _dalTag.GetAll(where, isTracking);
            return Mapper.Map<List<TagModel>>(entities);
        }


        public void AddRange(IEnumerable<TagModel> entities)
        {
            throw new NotImplementedException();
        }

        public void DeleteRange(IEnumerable<TagModel> entities)
        {
            throw new NotImplementedException();
        }

        public List<TagModel> GetAll(TagModel model, int page, int size, out int total, bool isTracking = true)
        {
            throw new NotImplementedException();
        }
    }
}
